package com.lanhao.aot.carrierintegration.naqel;

import java.io.File;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aliyun.openservices.shade.com.alibaba.fastjson.JSON;
import com.lanhao.aot.util.DateUtil;
import com.naqel.shipment.ArrayOfInt;
import com.naqel.shipment.AsrManifestShipmentDetails;
import com.naqel.shipment.AsrManifestShipmentDetailsAlt;
import com.naqel.shipment.AsrResult;
import com.naqel.shipment.ClientAddress;
import com.naqel.shipment.ClientContact;
import com.naqel.shipment.ClientInformation;
import com.naqel.shipment.ConsigneeInformation;
import com.naqel.shipment.ConsigneeInformationAlt;
import com.naqel.shipment.StickerSize;
import com.naqel.shipment.WaybillSurcharge;
import com.naqel.shipment.XMLShippingService;
import com.naqel.shipment.XMLShippingServiceSoap;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.UUID;

/**
 * 沙特阿拉伯
 * @author Administrator
 *
 */
public class NaqelUtil {
	private static final Logger LOG = LoggerFactory.getLogger(NaqelUtil.class);
	public static void main(String[] args) {
		doNaqelInvoke("KSA");
	}
	
	public static void doNaqelInvoke(String countryCode) {
		XMLShippingService service = new XMLShippingService();
		XMLShippingServiceSoap soap = service.getXMLShippingServiceSoap();
		
		AsrManifestShipmentDetailsAlt detail = getDetailParam(countryCode);
		
		LOG.info("开始调用Naqel请求运单信息, detail={}", JSON.toJSONString(detail));
		AsrResult result = soap.createWaybillForASRAlt(detail);
		LOG.info("结束调用创建运单结果result={}", JSON.toJSONString(result));
		
		boolean hasError = result.isHasError();
		if (hasError) {
			String message = result.getMessage();
			LOG.info("创建运单失败了，返回信息message={}", message);
			return;
		}
		int waybillNo = result.getWaybillNo();  // 236120475  -  236120476
		
		byte[] labelByte = soap.getWaybillSticker(detail.getClientInfo(), waybillNo, StickerSize.FOUR_M_SIXTH_INCHES);
		LOG.info("打印标签labelByte={}", labelByte);
		File destFile = FileUtil.writeBytes(labelByte, new File("D:/" + UUID.fastUUID().toString(true) + ".pdf"));
		LOG.info("保存文件成功，文件路径path={}", destFile.getAbsolutePath());
	}
	
	public static AsrManifestShipmentDetailsAlt getDetailParam(String countryCode) {
		
		AsrManifestShipmentDetailsAlt detail = new AsrManifestShipmentDetailsAlt();
		// TODO
		ClientInformation clientInfo = new ClientInformation();
		clientInfo.setClientID(9020077);
		clientInfo.setPassword("test1234");
		clientInfo.setVersion("9.0");
		
		ClientAddress clientAddress = new ClientAddress();
		ClientContact clientContact = new ClientContact();
		// 仓库信息
		if ("KSA".equals(countryCode)) {
			clientAddress.setCityCode("RUH");
			clientAddress.setCountryCode("KSA");
			clientAddress.setFirstAddress("BUILDING NO.3266,AL MADINAH ROAD");
			clientAddress.setPhoneNumber("00966-50-0603938");
//			clientAddress.setLocation("Client location");
//			clientAddress.setFax("123"); // Optional
//			clientAddress.setPOBox("123"); // Optional
//			clientAddress.setZipCode("12345"); // Optional

			clientContact.setName("MAHER");
			clientContact.setPhoneNumber("00966-50-0603938");
//			clientContact.setEmail("client@vip.com"); // Optional
//			clientContact.setMobileNo("512345678"); // Optional
			
		} else if ("AE".equals(countryCode)) {
			clientAddress.setCityCode("SHJ");
			clientAddress.setCountryCode("AE");
			clientAddress.setFirstAddress("WAREHOUSE NO.D18,FOURTH INDUSTRIALS RO");
			clientAddress.setPhoneNumber("00971-52-9767656");
//			clientAddress.setLocation("Client location");
//			clientAddress.setFax("123"); // Optional
//			clientAddress.setPOBox("123"); // Optional
//			clientAddress.setZipCode("12345"); // Optional

			clientContact.setName("FAROOQ");
			clientContact.setPhoneNumber("00971-52-9767656");
//			clientContact.setEmail("client@vip.com"); // Optional
//			clientContact.setMobileNo("512345678"); // Optional
		}
		
		clientInfo.setClientAddress(clientAddress);
		clientInfo.setClientContact(clientContact);
		detail.setClientInfo(clientInfo);
		
		// 发件人信息
		ConsigneeInformationAlt consigneeInfo = new ConsigneeInformationAlt();
		consigneeInfo.setConsigneeName("KunKun");
		consigneeInfo.setPhoneNumber("598765432");
		consigneeInfo.setAddress("Consignee pick up place");
		consigneeInfo.setCountryName("KSA");
		consigneeInfo.setCityName("Riyadh");
		consigneeInfo.setProvinceName("Riyadh");
		detail.setConsigneeInfoAlt(consigneeInfo);
		
		detail.setPicesCount(1);
//		detail.setWaybillNo(0);
//		detail.setOriginWaybillNo(0);
		detail.setRefNo(UUID.fastUUID().toString(true));
		detail.setBillingType(1);
		detail.setLoadTypeID(66);
		detail.setDeclareValue(0);
		detail.setCurrencyID(4);
		detail.setPickUpDate(DateUtil.convertToXMLGregorianCalendar("2022-04-09 00:00:00"));
		
		WaybillSurcharge waybillSurcharge = new WaybillSurcharge();
		ArrayOfInt a = new ArrayOfInt();
		a.getInt().add(9);
		waybillSurcharge.setSurchargeIDList(a);
		detail.setWaybillSurcharge(waybillSurcharge);
		
		detail.setGoodDesc("Goods need to return");
		// 发件人地址 留空
		detail.setDeliveryInstruction(""); // 收件地址 The location where the package will return to, or warehouse address.
		detail.setWeight(0.20d);
		detail.setInsuredValue(0);
		detail.setGoodsVATAmount(0d);
		detail.setIsCustomDutyPayByConsignee(false);
		
		return detail;
	}
}
